<?php
/**
 * Created by PhpStorm.
 * User: shutao
 * Date: 2018/9/13
 * Time: 18:11
 */

namespace wechat\models;

use tt\common\components\TtActiveRecord;
use yii\db\Migration;

/**
 * @property integer $id
 * @property integer $scene_id 微信二维码ID
 * @property integer $scene_type 渠道类型
 * @property string $nickname 昵称
 * @property string $mobile 手机号码
 * @property string $id_card_no 身份证号码
 * @property integer $status 状态
 * @property integer $group_id 分组id
 * @property integer $sex 性别
 * @property integer $birthday 生日
 * @property integer $area_id 地区id
 * @property string $avatar 头像
 * @property integer $created_at 创建日期
 * @property integer $updated_at 更新日期
 * @property string $weixin_unionid 微信开放平台 unionid
 * @property string $city 所在城市
 * @property string $province 所在省份
 * @property string $country 所在国家
 * @property integer $weixin_subscribed 是否关注
 * @property integer $weixin_subscribed_at 关注时间
 * @property integer $logins 登陆次数
 * @property integer $uid 用户的 id 号
 */
class WeUser extends TtActiveRecord
{
    public static function tableName()
    {
        return 'we_user';
    }

    public function createTableWithName(Migration $migration, $tableName)
    {
        $migration->createTable($tableName, [
            'id' => $migration->primaryKey()->unsigned(),
            'scene_id' => $migration->integer()->notNull()->defaultValue(0)->unsigned()->comment($this->getAttributeLabel('scene_id')),
            'scene_type' => $migration->tinyInteger(1)->notNull()->defaultValue(0)->unsigned()->comment($this->getAttributeLabel('scene_type')),
            'nickname' => $migration->string(100)->notNull()->defaultValue('')->comment($this->getAttributeLabel('nickname')),
            'mobile' => $migration->string(20)->notNull()->defaultValue('')->comment($this->getAttributeLabel('mobile')),
            'id_card_no' => $migration->string(40)->notNull()->defaultValue('')->comment($this->getAttributeLabel('id_card_no')),
            'status' => $migration->tinyInteger(1)->notNull()->defaultValue(0)->unsigned()->comment($this->getAttributeLabel('status')),
            'group_id' => $migration->tinyInteger()->notNull()->defaultValue(0)->unsigned()->comment($this->getAttributeLabel('group_id')),
            'sex' => $migration->tinyInteger(1)->notNull()->defaultValue(0)->unsigned()->comment($this->getAttributeLabel('sex')),
            'birthday' => $migration->integer(8)->notNull()->defaultValue(0)->unsigned()->comment($this->getAttributeLabel('birthday')),
            'area_id' => $migration->integer()->notNull()->defaultValue(0)->unsigned()->comment($this->getAttributeLabel('area_id')),
            'avatar' => $migration->string(255)->notNull()->defaultValue('')->comment($this->getAttributeLabel('avatar')),
            'created_at' => $migration->integer()->notNull()->defaultValue(0)->comment($this->getAttributeLabel('created_at')),
            'updated_at' => $migration->integer()->notNull()->defaultValue(0)->comment($this->getAttributeLabel('updated_at')),
            // 唯一
            'weixin_unionid' => $migration->string(100)->notNull()->defaultValue('')->comment($this->getAttributeLabel('weixin_unionid')),
            'city' => $migration->string(100)->notNull()->defaultValue('')->comment($this->getAttributeLabel('city')),
            'province' => $migration->string(100)->notNull()->defaultValue('')->comment($this->getAttributeLabel('province')),
            'country' => $migration->string(100)->notNull()->defaultValue('')->comment($this->getAttributeLabel('country')),
            'weixin_subscribed' => $migration->tinyInteger(1)->notNull()->defaultValue(0)->unsigned()->comment($this->getAttributeLabel('weixin_subscribed')),
            'weixin_subscribed_at' => $migration->integer()->notNull()->defaultValue(0)->unsigned()->comment($this->getAttributeLabel('weixin_subscribed_at')),
            'logins' => $migration->integer()->notNull()->unsigned()->defaultValue(0)->comment($this->getAttributeLabel('logins')),
            'uid' => $migration->integer()->notNull()->unsigned()->defaultValue(0)->comment($this->getAttributeLabel('uid'))
        ], 'CHARACTER SET utf8mb4 COLLATE=utf8mb4_unicode_ci');
        $migration->createIndex('mobile', $tableName, 'mobile');
        $migration->createIndex('weixin_unionid', $tableName, 'weixin_unionid');
        $migration->createIndex('uid', $tableName, 'uid');
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['status', 'group_id', 'sex', 'area_id', 'logins', 'weixin_subscribed', 'scene_id', 'scene_type', 'uid'], 'integer'],
            [['birthday', 'created_at', 'updated_at', 'weixin_subscribed_at'], 'safe'],
//            [['birthday', 'created_at', 'updated_at', 'last_logged_at', 'last_visited_at', 'weixin_subscribed_at'], 'safe'],
//            [['nickname', 'email', 'weixin_unionid'], 'string', 'max' => 100],
            [['nickname', 'weixin_unionid'], 'string', 'max' => 100],
            [['mobile'], 'string', 'max' => 20],
            [['id_card_no'], 'string', 'max' => 40],
            [['city', 'province', 'country'], 'string', 'max' => 50],
//            [['password', 'realname', 'city', 'province', 'country'], 'string', 'max' => 50],
            [['avatar'], 'string', 'max' => 255],
//            [['last_logged_ip'], 'string', 'max' => 15],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'scene_id' => '微信二维码ID',
            'scene_type' => '渠道类型',
            'nickname' => '昵称',
            'mobile' => '手机号码',
            'id_card_no' => '身份证号码',
            'status' => '状态',
            'group_id' => '分组id',
            'sex' => '性别',
            'birthday' => '生日',
            'area_id' => '地区id',
            'avatar' => '头像',
            'created_at' => '创建日期',
            'updated_at' => '更新日期',
            'weixin_unionid' => '开通微信开放平台时unionid',
            'city' => '所在城市',
            'province' => '所在省份',
            'country' => '所在国家',
            'weixin_subscribed' => '是否关注',
            'weixin_subscribed_at' => '关注时间',
            'logins' => '登陆次数',
            'uid' => '所关联的用户的 id 号'
            //            'email' => '邮箱',
//            'password' => '密码',
//            'realname' => '真实姓名'
//            'last_logged_ip' => '上次登录IP',
//            'last_logged_at' => '上一次登陆日期',
//            'last_visited_at' => '上一次访问日期',
        ];

    }


    public function beforeSave($insert)
    {
        if ($this->isNewRecord) {
            $this->created_at = time();
            // 注册为关联用户
        } else {
            $this->updated_at = time();
            $this->logins = $this->logins + 1;
        }
        return parent::beforeSave($insert);
    }

    public static function findByUnionid($unionid)
    {
        return WeUser::find()->where(['weixin_unionid' => $unionid])->one();
    }

    public static function findByUid($uid)
    {
        return WeUser::find()->where(['uid' => $uid])->one();
    }

}